const util = require('../utils/util')
export const baseRoot = "https://dev.ezhimian.com/"
export const baseUrl = "https://dev.ezhimian.com/api/weapp/"
//获取用户openid
export const userLogin = (params) => {
  let data = {
    url: baseUrl + `userlogin/`,
    data: params
  }
  return util.request(data)
}

//获取用户状态
export const userStatus = () => {
  let data = {
    url: baseUrl + `userstatus/`,
  }
  return util.requestToken(data)
}

//用户手机号登陆
export const mobileLogin = (params) => {
  let data = {
    url: baseUrl + `mobilelogin/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//提交睡眠情况
export const setSleepProblem = (params) => {
  let data = {
    url: baseUrl + `sleepproblem/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//AIS测评
export const ais = (params) => {
  let data = {
    url: baseUrl + `ais/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//压力测评
export const pressure = (params) => {
  let data = {
    url: baseUrl + `pressure/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//获取测评计划(有参数获取指定计划，无参数查询全部计划)
export const plans = (params) => {
  let data = {
    url: baseUrl + `plans/` + params + '/',
  }
  return util.requestToken(data)
}

//获取压力课程(有参数获取指定课程，无参数查询全部课程)
export const pressures = (params) => {
  let data = {
    url: baseUrl + `pressures/` + params + '/',
  }
  return util.requestToken(data)
}

//加入计划
export const joinPlan = (params) => {
  let data = {
    url: baseUrl + `joinplan/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//加入计划
export const joinPressure = (params) => {
  let data = {
    url: baseUrl + `joinpressure/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//创建微信支付订单接口
export const createunifiedorder = (params) => {
  let data = {
    url: baseUrl + `createunifiedorder/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

// 创建压力课程支付订单
export const createunifiedpressureorder = (params) => {
  let data = {
    url: baseUrl + `createunifiedpressureorder/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}


//获取最新的睡眠日记信息
export const lastsleepdiary = () => {
  let data = {
    url: baseUrl + `lastsleepdiary/`,
  }
  return util.requestToken(data)
}


//添加睡眠日记
export const commitsleepdiary = (params) => {
  let data = {
    url: baseUrl + `commitsleepdiary/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}



// 完成一天的计划
// 用户完成了自己当前的计划的一天的所有任务。
// 如果new_plan_id是 - 1，则没有推荐的计划，否则要弹出一个推荐的计划，id为new_plan_id。
// 如果medal_id是 - 1，则没有勋章，否则要弹出一个勋章，id为medal_id。
export const finishdayofplan = (params) => {
  let data = {
    url: baseUrl + `finishdayofplan/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}


export const finishdayofpressure = (params) => {
  let data = {
    url: baseUrl + `finishdayofpressure/`,
    data: params,
    method: 'post'
  }
  return util.requestToken(data)
}

//获取睡眠训练计划有天
export const daysofplan = () => {
  let data = {
    url: baseUrl + `daysofplan/`
  }
  return util.requestToken(data)
}


//获取压力课程包括的所有天
export const daysofpressure = () => {
  let data = {
    url: baseUrl + `daysofpressure/`
  }
  return util.requestToken(data)
}



//获取历史睡眠香度分数
export const scores = () => {
  let data = {
    url: baseUrl + `scores/`
  }
  return util.requestToken(data)
}


//获取当前任务所包括的详细内容
export const tasks = (params) => {
  let data = {
    url: baseUrl + `tasks/`,
    data: params
  }
  return util.requestToken(data)
}


//获取一周总结
export const weeksummary = (params) => {
  let data = {
    url: baseUrl + `weeksummary/`,
    data: params
  }
  return util.requestToken(data)
}


//获取我的计划
export const myPlan = (params) => {
  let data = {
    url: baseUrl + `myplan/`,
    data: params
  }
  return util.requestToken(data)
}

//获取我的压力课程
export const myPressure = (params) => {
  let data = {
    url: baseUrl + `mypressure/`,
    data: params
  }
  return util.requestToken(data)
}


//获取我的睡眠
export const mysleep = (params) => {
  let data = {
    url: baseUrl + `mysleep/`,
    data: params
  }
  return util.requestToken(data)
}

//获取我的徽章
export const mymedal = (params) => {
  let data = {
    url: baseUrl + `mymedal/`,
    data: params
  }
  return util.requestToken(data)
}



//获取食疗信息
export const diets = (params) => {
  let data = {
    url: baseUrl + `diets/` + params,
  }
  return util.requestToken(data)
}

//获取光疗信息
export const lights = (params) => {
  let data = {
    url: baseUrl + `lights/` + params,
  }
  return util.requestToken(data)
}


//获取理疗信息
export const pts = (params) => {
  let data = {
    url: baseUrl + `pts/` + params,
  }
  return util.requestToken(data)
}

//提交反馈意见
export const suggests = (suggestion) => {
  let data = {
    url: baseUrl + `addsuggestion/`,
    method: 'post',
    data: suggestion

  }
  return util.requestToken(data)
}


//第一次授权调用
export const getUserInfo = (that) => {
  wx.getUserInfo({
    success: function (res) {
      console.log('getUserInfo:', res)
      console.log('that:', that)
      that.setData({
        userInfo: res.userInfo,
        butnSet: false,
      })
      console.log(that.data.userInfo, '这是查看有没有获取用户信息的')
      console.log(that.data.butnSet, 'true用户信息按钮和false手机号按钮的显示')
      wx.setStorageSync('userInfo', res.userInfo)
      getOpenid(that)
    }
  })
}

//获取openid和session_key
export const getOpenid = (that) => {
  wx.login({
    success(res) {
      if (res.code) {
        let data = {
          code: res.code,
          userInfo: wx.getStorageSync('userInfo')
        }
        console.log('这是提交的数据:', data)
        userLogin(data).then(res => {
          that.setData({
            butnSet: false
          })
          wx.setStorageSync('openid', res.openid)
          console.log('openid:', res.openid)
          wx.setStorageSync('token', res.token)
          console.log('token:', res.token)
          let layout = wx.getStorageSync('layout')
          console.log('layout:', layout)
          console.log('that.route:', that.route)
          if (that.route == 'pages/home/home') {
            that.onLoad()
            that.onShow()
          } else {
            wx.switchTab({
              url: '/pages/home/home',
            })
          }
          wx.showToast({
            title: '登录成功！',
            icon: 'none'
          })
        })
      }
    }
  })
}

//手机号授权
export const getPhoneNumber = (e, that) => {
  if (e.detail.errMsg == "getPhoneNumber:ok") {
    const self = that;
    wx.login({
      success(res) {
        let data = {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          code: res.code
        }
        mobileLogin(data).then(res => {
          console.log(self)
          console.log(self.data.userInfo, '这是查看有没有获取用户信息的')
          console.log(self.data.butnSet, 'true用户信息按钮和false手机号按钮的显示')
          getApp().globalData.login = true
          if (res.errcode == '00000') {
            wx.removeStorageSync('layout')
            wx.switchTab({
              url: '/pages/home/home'
            })
          } else {
            wx.showToast({
              title: '服务异常，稍后重试',
              icon: 'none',
              mask: true
            })

          }
        })
      }
    })
  }
}


export const checkSession = (that) => {
  wx.checkSession({
    success() {
      getApp().globalData.login = true
      that.getOpenid()
    },
    fail() {
      that.getIsLogin()
    }
  })
}

export const getIsLogin = (that) => {
  let userInfo = wx.getStorageSync('userInfo')
  if (userInfo) {
    that.setData({
      butnSet: false,
      userInfo: userInfo
    })
  }
}